﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using LASGui.Classes;
using LASGui.MainService;

namespace LASGui.Views
{
    public partial class Reportes : Page
    {
        private readonly AuthenticatedUser _authenticatedUserManager = AuthenticatedUser.Instance;

        #region Navegacion

        /// <summary>
        /// Evento de navegacion que se dispara al terminar la navegación a esta página.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (_authenticatedUserManager.OnlineUser == null)
            {
                LogOff();
            }

            InitializeComponent();
            InputReportes.SelectionChanged += InputReportesSelectionChanged;
        }

        /// <summary>
        /// Método que obliga al usuario a salir de su sesión si se presenta un error de conexión o si intenta accesar a un zona reestringida sin haberse autenticado primero.
        /// </summary>
        public void LogOff()
        {
            _authenticatedUserManager.OnlineUser = null;
            NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
            MainPage mainPage = ((App) Application.Current).Mainpage;
            mainPage.HideMainMenu();
        }

        public void InputReportesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (InputReportes.SelectedIndex)
            {
                case 0:
                    GetBalanceDeComprobacion();
                    break;
                case 1:
                    GetEstadoResultados();
                    break;
                case 2:
                    GetBalanceGeneral();                    
                    break;
            }
        }

        #endregion

        #region Webservice Calls

        public void GetEstadoResultados()
        {
            var client = new MainServiceSilverlightClient();
            client.GetEstadoResultadosCompleted += ClientGetEstadoResultadosCompleted;
            client.GetEstadoResultadosAsync(_authenticatedUserManager.OnlineUser.CompanyId,
                                            (InputMoneda.SelectedItem as Label).Content.ToString());
        }

        public void GetBalanceGeneral()
        {
            var client = new MainServiceSilverlightClient();
            client.GetBalanceGeneralCompleted += ClientGetBalanceGeneralCompleted;
            client.GetBalanceGeneralAsync(_authenticatedUserManager.OnlineUser.CompanyId,
                                          (InputMoneda.SelectedItem as Label).Content.ToString());
        }

        public void GetBalanceDeComprobacion()
        {
            var client = new MainServiceSilverlightClient();
            client.GetBalanceDeComprobacionCompleted += ClientGetBalanceDeComprobacionCompleted;
            client.GetBalanceDeComprobacionAsync(_authenticatedUserManager.OnlineUser.CompanyId,
                                                 (InputMoneda.SelectedItem as Label).Content.ToString());
        }

        #endregion

        #region Webservice completeness events for asynchronous calls

        public void ClientGetBalanceDeComprobacionCompleted(object sender, GetBalanceDeComprobacionCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    ChildWindow errorWindow = new CustomChildWindow(e.Error);
                    errorWindow.Show();
                }
                else
                {
                    TextoReporte.Text = e.Result;
                }
            }
            catch (Exception)
            {
                var dialog = new CustomChildWindow(e.Error);
                dialog.Show();
            }
        }

        public void ClientGetBalanceGeneralCompleted(object sender, GetBalanceGeneralCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    ChildWindow errorWindow = new CustomChildWindow(e.Error);
                    errorWindow.Show();
                }
                else
                {
                    TextoReporte.Text = e.Result;
                }
            }
            catch (Exception)
            {
                var dialog = new CustomChildWindow(e.Error);
                dialog.Show();
            }
        }


        public void ClientGetEstadoResultadosCompleted(object sender, GetEstadoResultadosCompletedEventArgs e)
        {
            try
            {
                if (e.Error != null)
                {
                    ChildWindow errorWindow = new CustomChildWindow(e.Error);
                    errorWindow.Show();
                }
                else
                {
                    TextoReporte.Text = e.Result;
                }
            }
            catch (Exception)
            {
                var dialog = new CustomChildWindow(e.Error);
                dialog.Show();
            }
        }

        #endregion
    }
}